package concurrent.exchange;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @package concurrent.exchange
 * @Class Exchange
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-11-18 下午5:07
 */
public class Exchange {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        final Exchanger<String> ex = new Exchanger<>();
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String reStr = ex.exchange("一手交货");
                    System.out.println("吸毒者："+reStr);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    String reStr = ex.exchange("一手交钱");
                    System.out.println("贩毒者："+reStr);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        threadPool.shutdown();

    }
}
